//
//  SKMainViewController.m
//  Gastrosoph
//
//  Created by 张明磊 on 12-10-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SKMainViewController.h"
#import "SKHeaderFiles.h"
#import "SKMainFunctionViewController.h"
#import "SKRevealSideViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "SKMapShowViewController.h"
#import "SKShakeView.h"
#import "SKFoodTopView.h"
#import "SKFriendStateView.h"
#import "SKPhotographUpLoadViewController.h"
#import "MBProgressHUD.h"



typedef enum
{
    SKButtonTag_functionKey    = 0,
    SKButtonTag_photograph        ,
    SKButtonTag_mapShow           ,
    SKButtonTag_fillter           ,
    SKButtonTag_underTitle_left   ,
    SKButtonTag_underTitle_middle ,
    SKButtonTag_underTitle_right  ,
    
}SKMainView_button_tag;


typedef enum
{
    SKFillterButtonTag_comment = 0,
    SKFillterButtonTag_time       ,
    SKFillterButtonTag_location   ,
    SKFillterButtonTag_all        ,
    
}SKMainView_fillterButton_tag;




#define SK_BUTTON_FUNCTIONKEY_CGRECT CGRectMake(5, 4, 56, 36)

#define SK_BUTTON_PHOTOGRAPH_CGRECT CGRectMake(130, -3, 59, 59)

#define SK_BUTTON_MAPSHOW_CGRECT CGRectMake(225, 4, 42, 35)

#define SK_BUTTON_FILLTER_CGRECT CGRectMake(270, 4, 42, 35)

#define SK_VIEW_SHAKE CGRectMake(0, 101, 320, 359)

#define SK_VIEW_FOODTOP CGRectMake(0, 101, 320, 409)

#define SK_VIEW_FRIENDSTATE CGRectMake(0, 101, 320, 409)

#define SK_VIEW_FILLTER CGRectMake(0, 0, 320, 460)

#define SK_IMAGEVIEW_FILLTER CGRectMake(145, 40, 173, 211)

#define SK_BUTTON_ORDER_COMMENT CGRectMake(215, 70, 88, 30)

#define SK_BUTTON_ORDER_TIME CGRectMake(215, 115, 88, 30)

#define SK_BUTTON_ORDER_LOCATION CGRectMake(215, 160, 88, 30)

#define SK_BUTTON_ORDER_ALL CGRectMake(215, 205, 88, 30)



@interface SKMainViewController ()


@end

@implementation SKMainViewController

@synthesize view_navigationBar = _view_navigationBar;

@synthesize view_switchButton = _view_switchButton;

@synthesize button_underTitle_left = _button_underTitle_left;

@synthesize button_underTitle_middle = _button_underTitle_middle;

@synthesize button_underTitle_right = _button_underTitle_right;

@synthesize bool_cliclButton_mapShow = _bool_cliclButton_mapShow;

@synthesize mapShowViewController = _mapShowViewController;

@synthesize view_shakeView = _view_shakeView;

@synthesize view_foodTopView = _view_foodTopView;

@synthesize view_friendStateView = _view_friendStateView;

@synthesize bool_fillterButtonPressed = _bool_fillterButtonPressed;

@synthesize view_fillter = _view_fillter;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self install_view_foodTopTableView];
    
    [self install_view_switchButton];
    
    [self install_view_title];
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_shake_background"]]];
    
    _bool_cliclButton_mapShow = YES;
    
    _bool_fillterButtonPressed = YES;
	// Do any additional setup after loading the view.
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(install_functionView_preloadLeft) object:nil];
    
    [self performSelector:@selector(install_functionView_preloadLeft) withObject:nil afterDelay:0.3];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)dealloc
{
    [_button_underTitle_left release], _button_underTitle_left = nil;
    
    [_button_underTitle_middle release], _button_underTitle_middle = nil;
    
    [_button_underTitle_right release], _button_underTitle_right = nil;
    
    [_view_navigationBar release], _view_navigationBar = nil;
    
    [_view_switchButton release], _view_switchButton = nil;
    
    [_mapShowViewController release], _mapShowViewController = nil;
    
    [_view_shakeView release], _view_shakeView = nil;
    
    [_view_foodTopView release], _view_foodTopView = nil;
    
    [_view_fillter release], _view_fillter = nil;
    
    [super dealloc];
}

#pragma mark -
#pragma mark InstallView

- (void)install_view_title
{
    self.view_navigationBar = [[[UIView alloc] initWithFrame:SK_VIEW_NAVIGATION_CGRECT] autorelease];
    
    [_view_navigationBar setBackgroundColor:[UIColor clearColor]];
    
    SKCustomNavigationBar *customNavigationBar = [[SKCustomNavigationBar alloc] initWithFrame:SK_NAVIGATIONBAR_CGRECT];
    
    UIButton *button_functionKey = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_functionKey setFrame:SK_BUTTON_FUNCTIONKEY_CGRECT];
    
    [button_functionKey setBackgroundImage:[UIImage imageNamed:@"button_functionKey.png"] forState:UIControlStateNormal];
    
    [button_functionKey setBackgroundImage:[UIImage imageNamed:@"button_functionKey_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_functionKey setTag:SKButtonTag_functionKey];
    
    [button_functionKey addTarget:self action:@selector(didClickButton_MainView:) forControlEvents:UIControlEventTouchUpInside];
    
    [customNavigationBar addSubview:button_functionKey];
    
    UIButton *button_photograph = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_photograph setBackgroundImage:[UIImage imageNamed:@"button_photograph.png"] forState:UIControlStateNormal];
    
    [button_photograph setBackgroundImage:[UIImage imageNamed:@"button_photograph_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_photograph setFrame:SK_BUTTON_PHOTOGRAPH_CGRECT];
    
    [button_photograph setTag:SKButtonTag_photograph];
    
    [button_photograph addTarget:self action:@selector(didClickButton_MainView:) forControlEvents:UIControlEventTouchUpInside];
    
    [customNavigationBar addSubview:button_photograph];
    
    UIButton *button_mapShow = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_mapShow setFrame:SK_BUTTON_MAPSHOW_CGRECT];
    
    [button_mapShow setBackgroundImage:[UIImage imageNamed:@"button_mapShow.png"] forState:UIControlStateNormal];
    
    [button_mapShow setBackgroundImage:[UIImage imageNamed:@"button_mapShow_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_mapShow setTag:SKButtonTag_mapShow];
    
    [button_mapShow addTarget:self action:@selector(didClickButton_MainView:) forControlEvents:UIControlEventTouchUpInside];
    
    [customNavigationBar addSubview:button_mapShow];
    
    UIButton *button_fillter = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_fillter setFrame:SK_BUTTON_FILLTER_CGRECT];
    
    [button_fillter setBackgroundImage:[UIImage imageNamed:@"button_fillter.png"] forState:UIControlStateNormal];
    
    [button_fillter setBackgroundImage:[UIImage imageNamed:@"button_fillter_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_fillter setTag:SKButtonTag_fillter];
    
    [button_fillter addTarget:self action:@selector(didClickButton_MainView:) forControlEvents:UIControlEventTouchUpInside];
    
    [customNavigationBar addSubview:button_fillter];
    
    [_view_navigationBar addSubview:customNavigationBar];
    
    [self.view addSubview:_view_navigationBar];
    
    [customNavigationBar release];
}

- (void)install_view_switchButton
{
    _button_underTitle_left = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_button_underTitle_left setFrame:SK_BUTTON_UNDERTITLE_LEFT_CGRECT];
    
    [_button_underTitle_left setBackgroundImage:[UIImage imageNamed:@"button_underTitle_left.png"] forState:UIControlStateNormal];
    
    [_button_underTitle_left setTag:SKButtonTag_underTitle_left];
    
    [_button_underTitle_left addTarget:self action:@selector(didClickButton_SegmentControl:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_button_underTitle_left];
    
    _button_underTitle_middle = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_button_underTitle_middle setFrame:SK_BUTTON_UNDERTITLE_MIDDLE_CGRECT];
    
    [_button_underTitle_middle setBackgroundImage:[UIImage imageNamed:@"button_underTitle_middle_pressed.png"] forState:UIControlStateNormal];
    
    [_button_underTitle_middle setTag:SKButtonTag_underTitle_middle];
    
    [_button_underTitle_middle addTarget:self action:@selector(didClickButton_SegmentControl:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_button_underTitle_middle];
    
    _button_underTitle_right = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_button_underTitle_right setFrame:SK_BUTTON_UNDERTITLE_RIGHT_CGRECT];
    
    [_button_underTitle_right setBackgroundImage:[UIImage imageNamed:@"button_underTitle_right.png"]forState:UIControlStateNormal];
    
    [_button_underTitle_right setTag:SKButtonTag_underTitle_right];
    
    [_button_underTitle_right addTarget:self action:@selector(didClickButton_SegmentControl:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_button_underTitle_right];
}

- (void)install_view_foodTopTableView
{
    self.view_foodTopView = [[[SKFoodTopView alloc] initWithFrame:SK_VIEW_FOODTOP] autorelease];
    
    [self.view addSubview:_view_foodTopView];
}

- (void)install_view_friendStateTableView
{
    self.view_friendStateView = [[[SKFriendStateView alloc] initWithFrame:SK_VIEW_FRIENDSTATE] autorelease];
    
    [self.view addSubview:_view_friendStateView];
}




- (void)install_functionView_preloadLeft
{
    SKMainFunctionViewController *mainFunctionViewController = [[[SKMainFunctionViewController alloc] init] autorelease];
    
    [self.revealSideViewController preloadViewController:mainFunctionViewController forSide:PPRevealSideDirectionLeft withOffset:68.0];
    
    self.revealSideViewController.panInteractionsWhenClosed = PPRevealSideInteractionContentView;
    
    self.revealSideViewController.panInteractionsWhenOpened = PPRevealSideInteractionContentView;
    
}

- (void)install_fillterButtonWithFrame:(CGRect)_rect buttonTitle:(NSString *)_string buttonTag:(SKMainView_fillterButton_tag)_buttonTag
{
    UIButton *button_order_comment = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_order_comment setFrame:_rect];
    
    [button_order_comment.titleLabel setFont:[UIFont fontWithName:SK_TEXTFONT size:20.0f]];
    
    [button_order_comment setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [button_order_comment setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    [button_order_comment setTitle:_string forState:UIControlStateNormal];
    
    [button_order_comment addTarget:self action:@selector(didClickButton_fillter:) forControlEvents:UIControlEventTouchUpInside];
    
    [button_order_comment setTag:_buttonTag];
    
    [self.view_fillter addSubview:button_order_comment];
}

- (void)install_view_upLoadPhoto:(UIImage *)_image
{
    SKPhotographUpLoadViewController *controller = [[SKPhotographUpLoadViewController alloc] initWithImage:_image];
    
    [self presentViewController:controller animated:NO completion:nil];
    
    [controller release];
}

#pragma mark - 
#pragma mark DidClickButton

- (void)didClickButton_SegmentControl:(id)sender
{
    UIButton *button_tmp = (UIButton *)sender;
    
    switch (button_tmp.tag)
    {
        case SKButtonTag_underTitle_left:
        {
            [_button_underTitle_left setBackgroundImage:[UIImage imageNamed:@"button_underTitle_left_pressed.png"] forState:UIControlStateNormal];
            
            [_button_underTitle_middle setBackgroundImage:[UIImage imageNamed:@"button_underTitle_middle.png"] forState:UIControlStateNormal];
            
            [_button_underTitle_right setBackgroundImage:[UIImage imageNamed:@"button_underTitle_right.png"] forState:UIControlStateNormal];
            
            if(_view_shakeView)
            {
                _view_shakeView.accelerManager.delegate = nil;
            
                [_view_shakeView removeFromSuperview];
            }
            
            if(_view_foodTopView)
            {
                [_view_foodTopView removeFromSuperview];
            }
            
            if(_view_friendStateView)
            {
                [self.view addSubview:_view_friendStateView];
            }
            else
            {
                [self install_view_friendStateTableView];
            }
            
            break;
        }
            
        case SKButtonTag_underTitle_middle:
        {
            [_button_underTitle_left setBackgroundImage:[UIImage imageNamed:@"button_underTitle_left.png"] forState:UIControlStateNormal];
            
            [_button_underTitle_middle setBackgroundImage:[UIImage imageNamed:@"button_underTitle_middle_pressed.png"] forState:UIControlStateNormal];
            
            [_button_underTitle_right setBackgroundImage:[UIImage imageNamed:@"button_underTitle_right.png"] forState:UIControlStateNormal];
            
            if(_view_shakeView)
            {
                _view_shakeView.accelerManager.delegate = nil;
            
                [_view_shakeView removeFromSuperview];
            }
            
            if(_view_friendStateView)
            {
                [_view_friendStateView removeFromSuperview];
            }
            
            [self.view addSubview:_view_foodTopView];
            
            break;
        }
            
        case SKButtonTag_underTitle_right:
        {
            [_button_underTitle_left setBackgroundImage:[UIImage imageNamed:@"button_underTitle_left.png"] forState:UIControlStateNormal];
            
            [_button_underTitle_middle setBackgroundImage:[UIImage imageNamed:@"button_underTitle_middle.png"] forState:UIControlStateNormal];
            
            [_button_underTitle_right setBackgroundImage:[UIImage imageNamed:@"button_underTitle_right_pressed.png"] forState:UIControlStateNormal];
            
            if(_view_shakeView)
            {
                _view_shakeView.accelerManager.delegate = self.view_shakeView;
                
                [self.view addSubview:_view_shakeView];
            }
            
            else
            {
                self.view_shakeView = [[[SKShakeView alloc] initWithFrame:SK_VIEW_SHAKE] autorelease];
                
                _view_shakeView.accelerManager.delegate = self.view_shakeView;
                
                [self.view addSubview:_view_shakeView];
            }
  
            if(_view_foodTopView)
            {
                [_view_foodTopView removeFromSuperview];
            }
            
            if(_view_friendStateView)
            {
                [_view_friendStateView removeFromSuperview];
            }
            
            break;
        }
            
        default:
            break;
    }
}

- (void)didClickButton_MainView:(id)sender
{
    UIButton *button_tmp = (UIButton *)sender;
    
    switch (button_tmp.tag)
    {
        case SKButtonTag_functionKey:
        {
            SKMainFunctionViewController *mainFunctionViewController = [[[SKMainFunctionViewController alloc] init] autorelease];
            
            [self.revealSideViewController pushViewController:mainFunctionViewController onDirection:PPRevealSideDirectionLeft withOffset:68.0 animated:YES];
            
            self.revealSideViewController.panInteractionsWhenClosed = PPRevealSideInteractionContentView;
            
            self.revealSideViewController.panInteractionsWhenOpened = PPRevealSideInteractionContentView;
            
            break;
        }
        case SKButtonTag_photograph:
        {
            DLCImagePickerController *imagePickerViewController = [[[DLCImagePickerController alloc] init] autorelease];
            
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
            
            [imagePickerViewController setDelegate:self];
            
            [self presentViewController:imagePickerViewController animated:NO completion:nil];
            
            break;
        }
        case SKButtonTag_mapShow:
        {
            if(_bool_cliclButton_mapShow)
            {
                [button_tmp setBackgroundImage:[UIImage imageNamed:@"butto_mapShowReturnToMain.png"] forState:UIControlStateNormal];
                
                [button_tmp setBackgroundImage:[UIImage imageNamed:@"butto_mapShowReturnToMain_pressed.png"] forState:UIControlStateHighlighted];
                
                self.mapShowViewController = [[[SKMapShowViewController alloc] init] autorelease];
                
                [_view_navigationBar removeFromSuperview];
                
                [_mapShowViewController.view addSubview:_view_navigationBar];
                
                [self.view addSubview:_mapShowViewController.view];
                
                [_view_navigationBar setFrame:SK_VIEW_NAVIGATION_INMAPVIEW_CGRECT];
                
                self.revealSideViewController.panInteractionsWhenClosed = PPRevealSideInteractionNone;
            }
            
            else
            {
                [button_tmp setBackgroundImage:[UIImage imageNamed:@"button_mapShow.png"] forState:UIControlStateNormal];
                
                [button_tmp setBackgroundImage:[UIImage imageNamed:@"button_mapShow_pressed.png"] forState:UIControlStateHighlighted];
                
                [_view_navigationBar setFrame:SK_NAVIGATIONBAR_CGRECT];
                
                [_view_navigationBar removeFromSuperview];
                
                [_mapShowViewController.view removeFromSuperview];
                
                self.mapShowViewController = nil;
                
                [self.view addSubview:_view_navigationBar];
                
                self.revealSideViewController.panInteractionsWhenClosed = PPRevealSideInteractionContentView;
                
            }
            
            _bool_cliclButton_mapShow = !_bool_cliclButton_mapShow;
            
            break;
        }
        case SKButtonTag_fillter:
        {
            if(_bool_fillterButtonPressed)
            {
                if(_view_fillter)
                {
                    [self.view addSubview:_view_fillter];
                }
                
                else
                {
                    self.view_fillter = [[[UIView alloc] initWithFrame:SK_VIEW_FILLTER] autorelease];
                    
                    UIImageView *imageView_fillter = [[UIImageView alloc] initWithFrame:SK_IMAGEVIEW_FILLTER];
                    
                    [imageView_fillter setImage:[UIImage imageNamed:@"imageView_fillter.png"]];

                    [_view_fillter addSubview:imageView_fillter];
                    
                    [imageView_fillter release];
                    
                    [self install_fillterButtonWithFrame:SK_BUTTON_ORDER_COMMENT buttonTitle:@"只看最棒" buttonTag:SKFillterButtonTag_comment];
                    
                    [self install_fillterButtonWithFrame:SK_BUTTON_ORDER_TIME buttonTitle:@"只看最新" buttonTag:SKFillterButtonTag_time];
                    
                    [self install_fillterButtonWithFrame:SK_BUTTON_ORDER_LOCATION buttonTitle:@"只看身边" buttonTag:SKFillterButtonTag_location];
                    
                    [self install_fillterButtonWithFrame:SK_BUTTON_ORDER_ALL buttonTitle:@"查看全部" buttonTag:SKFillterButtonTag_all];
                    
                    [self.view addSubview:_view_fillter];
                }
            }
            
            else
            {
                [_view_fillter removeFromSuperview];
            }
            
            _bool_fillterButtonPressed = !_bool_fillterButtonPressed;
            
            break;
        }
        default:
            break;
    }
}

- (void)didClickButton_fillter:(id)sender
{
    UIButton *button_tmp = (UIButton *)sender;
    
    switch (button_tmp.tag)
    {
        case SKFillterButtonTag_comment:
        {
            NSLog(@"test");
            
            break;
        }
            
        case SKFillterButtonTag_time:
        {
            break;
        }
            
        case SKFillterButtonTag_location:
        {
            break;
        }
            
        case SKFillterButtonTag_all:
        {
            break;
        }
            
        default:
            break;
    }
}

#pragma mark -
#pragma mark DLCImagePickerDelegate

-(void) imagePickerControllerDidCancel:(DLCImagePickerController *)picker
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
    
    [self dismissModalViewControllerAnimated:YES];
}



-(void) imagePickerController:(DLCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
    
    [self dismissModalViewControllerAnimated:NO];
    
    [self view_showProgressHUD:@"正在处理相片,请稍后"];
    
    if (info)
    {
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageDataToSavedPhotosAlbum:[info objectForKey:@"data"] metadata:nil completionBlock:^(NSURL *assetURL, NSError *error)
         {
             if (error)
             {
                 NSLog(@"ERROR: the image failed to be written");
             }
             else
             {
            
                 UIImage *image_photo = [UIImage imageWithData:[info objectForKey:@"data"]];
                 
                 [self install_view_upLoadPhoto:image_photo];
  
                 NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
             }
         }];
    }
}

#pragma mark -
#pragma mark MBProgressHUD

- (void)view_showProgressHUD:(NSString *) _infoContent
{
    
    
    MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:[self view] animated:YES];
    
    [progressHUD setLabelText:_infoContent];
    
    [progressHUD setLabelFont:[UIFont fontWithName:SK_TEXTFONT size:13.0f]];
    
    [progressHUD setRemoveFromSuperViewOnHide:YES];
    
    [self performSelector:@selector(view_hideProgressHUD) withObject:nil afterDelay:2.0f];
}

- (void)view_hideProgressHUD
{
    [MBProgressHUD hideHUDForView:[self view] animated:YES];
}



#pragma mark - 
#pragma mark TouchView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(!_bool_fillterButtonPressed)
    {
        [_view_fillter removeFromSuperview];
        
        _bool_fillterButtonPressed = !_bool_fillterButtonPressed;
    }
}

@end
